#include "precompiled.h"
#include "message.h"

namespace Opl
{
	namespace Protocol
	{

		using Opl::Core::EntityObject;

		Message Message::fromByteArray(const QByteArray& bytes)
		{
			Message msg;
			QDataStream stream (bytes);
			stream >> msg;
			return msg;
		}

		QDataStream& operator << (QDataStream& stream, const Message& msg)
		{
			stream << msg.header();
			stream << msg.data();
			return stream;
		}

		QDataStream& operator >> (QDataStream& stream, Message& msg)
		{
			Header h;
			stream >> h;
			msg.setHeader(h);

			QByteArray a;
			stream >> a;
			msg.setData(a);
			return stream;
		}

		const QByteArray& Message::data()const
		{
			return msg.data;
		}
		void Message::setData(const QByteArray& d)
		{
			msg.data = d;
		}

		void Message::setHeader(const Header& hdr)
		{
			msg.hdr = hdr;
		}

		const Header& Message::header()const
		{
			return msg.hdr;
		}

		Message& Message::operator=(const Message& that)
		{
			this->msg.hdr = that.msg.hdr;
			this->msg.data = that.msg.data;
			return *this;
		}


		Message::Message(const Message& msg):isvalid(false)
		{
			(*this) = msg;
		}

		Message::Message(const QByteArray& bytes):isvalid(false)
		{
			(*this) = Message::fromByteArray(bytes);
		}

		
		Message::Message(EntityObject* parent) :
			isvalid(false)
		{
			//d->
		}

		Message::~Message()
		{
		}

		bool Message::isValid()
		{
			return msg.hdr.Size() > 0;
		}

	}
}